/* Check JER_Erasures_To_Erased(). */
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "reed_sol-2.h"
#include "jerasure-2.h"
#include "galois-2.h"

using namespace std;

int main( int argc, char** argv )
{
	JER_Bitmatrix *jbm;
	JER_Matrix *jm;
	JER_Slices *slices;
	int i, j;

	int m = atoi( argv[1] );
	int k = atoi( argv[2] );
	int w = 8 << ( atoi( argv[3] ) % 3 );
	int tmp;
	unsigned char* buffer;
	int size;
	vector <int> erasures;
	vector <int> erased;

	slices = new JER_Slices();
	slices->W = w;
	slices->K = k;
	slices->M = m;
	slices->PS = 8;
	slices->size = 8; //slices->PS * w;
	size = slices->size;
	slices->ptrs.resize( k + m );
	buffer = (unsigned char*) malloc( sizeof(char) * (k+m) * size );
	srand48(m);
	for( i = 0; i < k*size; i++ )
	{
		buffer[i] = lrand48() % 256;
		if( i % size == 0 )
			fprintf( stdout, "\n" );
		fprintf( stdout, "%x ", buffer[i] );
	}
	fprintf( stdout, "\n" );
	for( i = 0; i < k+m; i++ )
		slices->ptrs[i] = buffer + i * size;
	jm = reed_sol_vandermonde_coding_matrix( k, m, w );
	JER_Matrix_Encode( slices, jm );

	for( i = 0; i < (k+m)*size; i++ )
	{
		if( i % size == 0 )
			fprintf( stdout, "\n" );
		fprintf( stdout, "%x ", buffer[i] );
	}
	fprintf( stdout, "\n" );
	for( i = 0; i < m; i++ )
	{
		tmp = lrand48() % (k+m);
		for( j = 0; j < slices->failures.size() && 
					slices->failures[j] != tmp; j++ );
		if( j == slices->failures.size() )
		{
			slices->failures.push_back( tmp );
			memset( slices->ptrs[tmp], 0, size );
		}
	}
	for( i = 0; i < (k+m)*size; i++ )
	{
		if( i % size == 0 )
			fprintf( stdout, "\n" );
		fprintf( stdout, "%x ", buffer[i] );
	}
	fprintf( stdout, "\n" );

	JER_Matrix_Decode( slices, jm, 0, slices->failures );

	for( i = 0; i < (k+m) * size; i++ )
	{
		if( i % size == 0 )
			fprintf( stdout, "\n" );
		fprintf( stdout, "%x ", buffer[i] );
	}
	fprintf( stdout, "\n" );
	return 0;
}
